﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Models
{
    public class TokenModel
    {
        private PartModel _firstPart = new PartModel();
        private PartModel _secondPart = new PartModel();

        public int Id { get; set; }
        public string Name { get; set; }
        public TimeTableModel OwnTimeSheet { get; set; }
        public GroupModel OwnGroup { get; set; }
        public PartModel FirstPart { get { return _firstPart; } set { _firstPart = value; } }
        public PartModel SecondPart { get { return _secondPart; } set { _secondPart = value; } }

        public DepartureModel ShiftDep
        {
            get; set;
        }

        public TokenModel()
        {
        }

        public TokenModel(string name)
        {
            Name = name;
        }

        #region Function


        /// <summary>
        /// 加入发车时间至路牌,
        /// 自动判断插入的部分
        /// </summary>
        /// <param name="departureModel"></param>
        /// <returns></returns>
        public bool AddDep(DepartureModel departureModel)
        {
            bool result = false;

            if (SecondPart.IsEmpty && !FirstPart.IsEnded)
            {
                departureModel.OwnPart = this.FirstPart;
                result = FirstPart.AddDep(departureModel);
            }
            else if (!SecondPart.IsEmpty || (SecondPart.IsEmpty && FirstPart.IsEnded))
            {
                departureModel.OwnPart = this.SecondPart;
                result = SecondPart.AddDep(departureModel);
            }

            //result = SecondPart.IsEmpty ? SecondPart.AddDep(departureModel) : FirstPart.AddDep(departureModel);

            return result;
        }

        /// <summary>
        /// 路牌开始
        /// 也可能是路牌中的上半班或下半班开始
        /// </summary>
        /// <param name="tokenPosition"></param>
        public void StartUp(int tokenPosition)
        {
            if (FirstPart.IsEnded && SecondPart.IsEnded)
            {
                return;
            }

            if (FirstPart.IsEnded)
            {
                SecondPart.Start(tokenPosition);
            }
            else
            {
                FirstPart.Start(tokenPosition);
            }
        }

        /// <summary>
        /// 路牌结束
        /// 也可能是路牌中的上半班或下半班结束
        /// </summary>
        public void End()
        {
            if (SecondPart.IsEmpty)
            {
                FirstPart.End();
            }
            else
            {
                SecondPart.End();
            }
        }

        #endregion

    }
}
